package com.example.itemdecoration;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.itemdecoration.adapter.MyAdpter;
import com.example.itemdecoration.decoration.MyDecoration;
import com.example.itemdecoration.service.MyService;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements BaseQuickAdapter.OnItemClickListener {

    private RecyclerView recyclerView;
    private MyAdpter myAdpter;
    private List<String> list = new ArrayList<>();
    private MyHeadView myHeadView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recycle_view);
        myAdpter = new MyAdpter(this, list);
        myHeadView = new MyHeadView(this);
        recyclerView.addItemDecoration(new MyDecoration(this));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(myAdpter);
        myAdpter.addHeaderView(myHeadView.getmView());
        initDate();
        myAdpter.setOnItemClickListener(this);
    }

    private void initDate() {
        for (int i = 0; i < 100; i++) {
            list.add(i + "s");
        }
        myAdpter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }


    public static class MyHeadView implements View.OnClickListener {
        private View mView;
        private TextView mTvStart, mTvStop, mTvBind, mTvUnBind, mTvSend;
        private Context mContext;
        private MyServiceConnertion serviceConnertion;
        private Messenger sendMessenger;

        private boolean isStopService=true;

        public MyHeadView(Context context) {
            if (mView == null) {
                mContext = context;
                mView = LayoutInflater.from(context).inflate(R.layout.head_view, null, false);

                mTvStart = mView.findViewById(R.id.tv_start);
                mTvStop = mView.findViewById(R.id.tv_stop);
                mTvBind = mView.findViewById(R.id.tv_bind);
                mTvUnBind = mView.findViewById(R.id.tv_unbind);
                mTvSend = mView.findViewById(R.id.tv_send);

                serviceConnertion = new MyServiceConnertion();

                mTvStop.setOnClickListener(this);
                mTvStart.setOnClickListener(this);
                mTvBind.setOnClickListener(this);
                mTvUnBind.setOnClickListener(this);
                mTvSend.setOnClickListener(this);

            }
        }


        public View getmView() {
            return mView;
        }

        class MyServiceConnertion implements ServiceConnection {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d("TAOTAO", "onServiceConnected 已启动！");
                sendMessenger = new Messenger(service);
                isStopService = false;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d("TAOTAO", "onServiceDisconnected 已启动！");
                sendMessenger = null;
                isStopService = true;
            }
        }


        @Override
        public void onClick(View v) {
            Intent intent = new Intent(mContext, MyService.class);
            if (v.getId() == R.id.tv_start) {
                mContext.startService(new Intent(mContext, MyService.class));
            } else if (v.getId() == R.id.tv_stop) {
                mContext.stopService(new Intent(mContext, MyService.class));
            } else if (v.getId() == R.id.tv_bind) {
                mContext.bindService(intent, serviceConnertion, mContext.BIND_AUTO_CREATE);
            } else if (v.getId() == R.id.tv_unbind) {
                mContext.unbindService(serviceConnertion);
            } else if (v.getId() == R.id.tv_send) {
                sendMsg();
            }
        }

        //发消息并接收MyService的回复
        private void sendMsg() {
            if (!isStopService) {
                Message message = Message.obtain(null, 1, 0, 0);
                message.replyTo = replyMssenger;
                try {
                    sendMessenger.send(message);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }

        private Messenger replyMssenger = new Messenger(new ReplyMessage());

        private static class ReplyMessage extends Handler {
            @Override
            public void handleMessage(@NonNull Message msg) {
                Log.d("TAOTAO", "ReplyMessage:" + msg.getData());
                Bundle bundle = msg.getData();
                Log.d("TAOTAO", "KEY=" + bundle.get("key"));
                super.handleMessage(msg);
            }
        }
    }

}
